package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author WSJ
 * @Date 2022-03-26-20:25
 * @Version 1.0 （版本号）
 */
@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {
    @Autowired
    private MongoTemplate template;
    @Override
    @Async
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        //查看是否已有记录
        Query query = new Query();
        query.addCriteria(Criteria.where("keyword").is(keyword));
        if (userId!=null) {
            query.addCriteria(Criteria.where("userId").is(userId));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        long count = template.count(query, ApUserSearch.class);
        if (count==0) {
            //不存在，添加记录
            ApUserSearch search = new ApUserSearch();
            search.setCreatedTime(new Date());
            search.setKeyword(keyword);
            if (userId!=null) {
                search.setUserId(userId);
            }else {
                search.setEquipmentId(equipmentId);
            }
            template.save(search);
        }
    }

    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        //加载搜索记录
        //构建查询条件
        Query query = new Query();
        User user = AppThreadLocalUtil.get();
        if (user!=null) {
            query.addCriteria(Criteria.where("userId").is(user.getId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        //按时间排序，查询5条
        Pageable page= PageRequest.of(0,5, Sort.Direction.DESC,"createdTime");
        query.with(page);
        List<ApUserSearch> searchList = template.find(query, ApUserSearch.class);
        return ResponseResult.okResult(searchList);
    }

    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> list = new ArrayList<>();
        for (ApUserSearch search : hisList) {
            String id = search.getId();
            list.add(id);
        }
        //构建删除条件
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(list));
        template.remove(query,ApUserSearch.class);
        //返回
        return ResponseResult.okResult();
    }
}
